package NextClass.Native;

import com.sun.net.httpserver.HttpExchange;
import expression.StringExpression;
import vm.baseNativeUseFunction.FileToObjectConverter;
import vm.runtime.NextVMRuntime;


import java.io.ByteArrayOutputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.Serializable;

import static NextClass.Native.HttpRun.getBodyAsByteArray;
import static NextClass.Native.HttpRun.getHeadersAsString;

public class Res implements Serializable {
    public static String[] ClassMethods = {
        "URL",
        "method",
        "head",
        "body",
        "output",
        "IP",
        "code",
        "set_head",
        "cors",
        "return"
    };
    public static byte[] convertToBytes(Object obj) throws Exception {
        // 创建一个字节数组输出流
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        // 创建一个对象输出流，用于将对象写入到字节数组输出流
        ObjectOutputStream out = new ObjectOutputStream(bos);
        // 将对象写入到字节数组输出流
        out.writeObject(obj);
        // 关闭对象输出流
        out.close();
        // 返回字节数组输出流中的字节数组
        return bos.toByteArray();
    }
    public static void sendHead(HttpExchange exchange, String key,String value) throws Exception {
        // 发送自定义的HTTP响应头
        exchange.getResponseHeaders().set(key,value);
    }
    public static Object run(String Invoke, Object object,NextVMRuntime nextVMRuntime, Object... objects) throws Exception{
        HttpExchange httpExchange = (HttpExchange) object;
        if (Invoke.equals("URL")) {
            return java.net.URLDecoder.decode(httpExchange.getRequestURI().toString());
        }
        else if (Invoke.equals("method")) {
            return httpExchange.getRequestMethod();
        }
        else if (Invoke.equals("head")) {
            return getHeadersAsString(httpExchange);
        }
        else if (Invoke.equals("body")) {
            return getBodyAsByteArray(httpExchange);
        }
        else if (Invoke.equals("IP")) {
            return httpExchange.getRemoteAddress().getAddress().toString();
        }
        else if (Invoke.equals("code")) {
            httpExchange.sendResponseHeaders(Integer.parseInt(String.valueOf(objects[0])) , 0);
            return null;
        }
        else if (Invoke.equals("set_head")) {
            sendHead(httpExchange , String.valueOf(objects[0]) , String.valueOf(objects[1]));
            return null;
        }
        else if (Invoke.equals("output")) {
            return httpExchange.getResponseBody();
        }
        else if (Invoke.equals("cors")) {
            httpExchange.getResponseHeaders().add(
                    "Access-Control-Allow-Origin",
                    "*");
            httpExchange.getResponseHeaders().add(
                    "Access-Control-Allow-Methods",
                    "POST");
            httpExchange.getResponseHeaders().add(
                    "Access-Control-Allow-Headers",
                    "*");

            return null;
        }
        else if (Invoke.equals("return")){
            int code = Integer.parseInt(String.valueOf(objects[0]));
            OutputStream outputStream = httpExchange.getResponseBody();
            httpExchange.sendResponseHeaders(code , 0);
            if (objects[1] instanceof byte[]) {
                outputStream.write((byte[]) objects[1]);
                outputStream.flush();
                outputStream.close();
            }
            else if (objects[1] instanceof String || StringExpression.IsNumber( String.valueOf(objects[1])) ) {
                outputStream.write(String.valueOf(objects[1]).getBytes());
                outputStream.flush();
                outputStream.close();
            }
            else {
                outputStream.write(convertToBytes(objects[1]));
                outputStream.flush();
                outputStream.close();
            }
            return null;
        }
        else {
            return null;
        }
    }
}
